package com.huilink.webmail.service.impl;

import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;

import com.huilink.db.HrDbServiceFactory;
import com.huilink.db.WebMailDAO;
import com.huilink.webmail.service.api.WebMailService;
import com.huilink.webmail.service.api.WebMailServiceFactory;

public class WebMailServiceFactoryImpl extends WebMailServiceFactory {

	

	@Override
	protected void configure(Map<String, Object> map) {
		// TODO Auto-generated method stub

	}

	

	@Override
	public WebMailService createWebMailService() {
		if (entityManagerFactory == null)
			throw new IllegalStateException(
					"need set entityManagerFactory first");
		Executor mailSenderExecutor = new ThreadPoolExecutor(1, 1, 10000,
				TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(100),
				new CallerRunsPolicy());
		WebMailDAO webMailDao = HrDbServiceFactory.getInstance()
				.createWebMailDAO();

		return (WebMailService) (Proxy.newProxyInstance(WebMailService.class
					.getClassLoader(), new Class[] { WebMailService.class },
					new WebMailServiceProxy(entityManagerFactory,
							webMailDao, mailSenderExecutor)));
		
	}

}
